package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanySysNoticeAddDTO;
import org.jsola.hr.dto.CompanySysNoticeOfflineDTO;
import org.jsola.hr.dto.CompanySysNoticeUpdateDTO;
import org.jsola.hr.query.CompanySysNoticeQuery;
import org.jsola.hr.service.ICompanySysNoticeService;
import org.jsola.hr.service.IUserReadCompanySysNoticeService;
import org.jsola.hr.vo.CompanySysNoticeListVO;
import org.jsola.hr.vo.CompanySysNoticeVO;
import org.jsola.hr.vo.UserCompanySysNoticeListVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 公司系统公告信息
 *
 * @author zhr
 */
@RestController("hrCompanySysNoticeController")
@RequestMapping(HrConstants.API_PATH + "/v1/companySysNotices")
public class CompanySysNoticeController implements ICompanySysNoticeControllerAPI {

    @Autowired
    private ICompanySysNoticeService companySysNoticeService;

    @Autowired
    private IUserReadCompanySysNoticeService userReadCompanySysNoticeService;

    @Override
    @PostMapping
    public Result<CompanySysNoticeVO> save(@Valid @RequestBody CompanySysNoticeAddDTO companySysNoticeAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司系统公告信息权限校验

        return Result.success(
                companySysNoticeService.save(companySysNoticeAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanySysNoticeUpdateDTO companySysNoticeUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司系统公告信息权限校验

        int count = companySysNoticeService.update(companySysNoticeUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{companySysNoticeId}")
    public Result<Integer> delete(@PathVariable String companySysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司系统公告信息权限校验

        int count = companySysNoticeService.deleteByIds(tokenUser, companySysNoticeId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] companySysNoticeIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除公司系统公告信息权限校验

        int count = companySysNoticeService.deleteByIds(tokenUser, companySysNoticeIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companySysNoticeId}")
    public Result<CompanySysNoticeVO> findById(@PathVariable String companySysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司系统公告信息权限校验

        return Result.success(
            companySysNoticeService.selectById(companySysNoticeId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanySysNoticeListVO>> listAll(@Valid CompanySysNoticeQuery companySysNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司系统公告信息权限校验

        return Result.success(
            companySysNoticeService.select(companySysNoticeQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanySysNoticeListVO>> page(@Valid CompanySysNoticeQuery companySysNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            companySysNoticeService.selectPage(companySysNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "/onlineNotice/{companySysNoticeId}")
    public Result<String> onlineNotice(@PathVariable String companySysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();

        companySysNoticeService.onlineNotice(companySysNoticeId, tokenUser);
        return Result.success("上线成功");
    }

    @Override
    @PutMapping(value = "/offlineNotice/{companySysNoticeId}")
    public Result<String> offlineNotice(@PathVariable String companySysNoticeId,
                                        @Valid @RequestBody CompanySysNoticeOfflineDTO companySysNoticeOfflineDTO) {
        TokenUser tokenUser = UserContext.getUser();

        companySysNoticeService.offlineNotice(companySysNoticeId, companySysNoticeOfflineDTO, tokenUser);
        return Result.success("下线成功");
    }

    @Override
    @PutMapping(value = "/topNotice/{companySysNoticeId}")
    public Result<String> topNotice(@PathVariable String companySysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();

        companySysNoticeService.topNotice(companySysNoticeId, tokenUser);
        return Result.success("公告置顶成功");
    }

    @Override
    @PutMapping(value = "/upTopNotice/{companySysNoticeId}")
    public Result<String> upTopNotice(@PathVariable String companySysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();

        companySysNoticeService.upTopNotice(companySysNoticeId, tokenUser);
        return Result.success("公告取消置顶成功");
    }


    @Override
    @GetMapping(value = "/pageByCompany/{companyId}")
    public Result<Page<CompanySysNoticeListVO>> pageByCompany(@PathVariable String companyId,
                                                              @Valid CompanySysNoticeQuery companySysNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        companySysNoticeQuery.setCompanyId(companyId);
        return Result.success(
                companySysNoticeService.selectPage(companySysNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listSysNoticeByUser/{companyId}")
    public Result<List<UserCompanySysNoticeListVO>> listSysNoticeByUser(@PathVariable String companyId,
                                                                        @Valid CompanySysNoticeQuery companySysNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        companySysNoticeQuery.setCompanyId(companyId);
        return Result.success(
                companySysNoticeService.listSysNoticeByUser(companySysNoticeQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/pageSysNoticeByUser/{companyId}")
    public Result<Page<UserCompanySysNoticeListVO>> pageSysNoticeByUser(@PathVariable String companyId,
                                                                        @Valid CompanySysNoticeQuery companySysNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        companySysNoticeQuery.setCompanyId(companyId);
        return Result.success(
                companySysNoticeService.pageSysNoticeByUser(companySysNoticeQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/findSysNoticeByUser/{sysNoticeId}")
    public Result<UserCompanySysNoticeListVO> findSysNoticeByUser(@PathVariable String sysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companySysNoticeService.findSysNoticeByUser(sysNoticeId, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/readSysNotice/{sysNoticeId}")
    public Result<String> readSysNotice(@PathVariable String sysNoticeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        userReadCompanySysNoticeService.readSysNotice(sysNoticeId, tokenUser);
        return Result.success("修改成功");
    }

    @Override
    @DeleteMapping(value = "/deleteUserSysNotice")
    public Result<String> deleteUserSysNotice(@RequestBody String[] sysNoticeIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        userReadCompanySysNoticeService.deleteUserSysNotice(tokenUser, sysNoticeIds);
        return Result.success("删除成功");
    }

    /**
     * TODO 发送带跳转的通知,测试接口
     * @return
     */
    @PostMapping(value = "/sendMessageTest")
    public Result<String> sendMessageTest() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        companySysNoticeService.sendMessageTest(tokenUser);
        return Result.success("发送成功");
    }

    /**
     * TODO 发送不带跳转的通知,测试接口
     * @return
     */
    @PostMapping(value = "/sendMessageTestNoJump")
    public Result<String> sendMessageTestNoJump() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        companySysNoticeService.sendMessageTestNoJump(tokenUser);
        return Result.success("发送成功");
    }
}

